package com.gensdai.leliang.view.banner;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.adapter.MainMenuAdapterRec;
import com.gensdai.leliang.common.bean.CategoryBean;
import com.gensdai.leliang.view.banner.widget.Banner.BaseIndicaorBanner;

import java.util.LinkedList;
import java.util.List;

/**
 * 简单的banner
 */
public class SimpleMenu extends
        BaseIndicaorBanner<List<CategoryBean>, SimpleMenu> {
    private ColorDrawable colorDrawable;
    onMenuClickListner click;
    public SimpleMenu(Context context) {
        this(context, null, 0);
    }

    public SimpleMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        colorDrawable = new ColorDrawable(Color.parseColor("#555555"));
    }

    public void setOnMenuClickListener(onMenuClickListner click){
        this.click = click;
    }

    @Override
    public View onCreateItemView(int position) {
        View inflate = View.inflate(context, R.layout.adapter_simple_banner_menu,
                null);
        RecyclerView rec = (RecyclerView)inflate.findViewById(R.id.menurec);
        initRecyclerView(rec);
        List<CategoryBean> beans = list.get(position);

        GridLayoutHelper layoutHelper = new GridLayoutHelper(3);
        layoutHelper.setMargin(0, 0, 0, 0);
        layoutHelper.setAutoExpand(false); //关闭自动扩展
        layoutHelper.setBgColor(ContextCompat.getColor(getContext(), R.color.bgColor_new));

//        layoutHelper.setPadding(getResources().getDimensionPixelOffset(R.dimen.px34dp),getResources().getDimensionPixelOffset(R.dimen.px24dp),getResources().getDimensionPixelOffset(R.dimen.px34dp),getResources().getDimensionPixelOffset(R.dimen.px24dp));
        layoutHelper.setPadding(0,getResources().getDimensionPixelOffset(R.dimen.px24dp),0,getResources().getDimensionPixelOffset(R.dimen.px24dp));
        //横间距
        layoutHelper.setHGap(0);
        //垂直间距
        layoutHelper.setVGap(getResources().getDimensionPixelOffset(R.dimen.px20dp));
        if (adapters != null) {
            menuAdapter = new MainMenuAdapterRec(getContext(), layoutHelper, beans);
            menuAdapter.setOnMenuTypeClick(new MainMenuAdapterRec.MenuTypeOnclickListener<CategoryBean>() {
                @Override
                public void OnPositionClick(CategoryBean menu, int position) {
                    if (click != null) {
                        click.onMenuClick(menu.categoryNo);
                    }
                }
            });

            if(adapters != null && adapters.size() > 0){

            }else{
                adapters.add(menuAdapter);
                //初始化完毕后开始适配
                delegateAdapter.setAdapters(adapters);
                delegateAdapter.notifyDataSetChanged();
            }

        }

        return inflate;
    }

    MainMenuAdapterRec menuAdapter;
    DelegateAdapter delegateAdapter;
    List<DelegateAdapter.Adapter> adapters;
    private void initRecyclerView(RecyclerView recyclerView) {
        //创建线性布局
        final VirtualLayoutManager layoutManager = new VirtualLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        // layoutManager.setReverseLayout(true);

        RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = ((VirtualLayoutManager.LayoutParams) view.getLayoutParams()).getViewPosition();
                outRect.set(4, 4, 4, 4);
            }
        };

        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

        recyclerView.setRecycledViewPool(viewPool);

        // recyclerView.addItemDecoration(itemDecoration);
        viewPool.setMaxRecycledViews(0, 20);

        delegateAdapter = new DelegateAdapter(layoutManager, false);

        recyclerView.setAdapter(delegateAdapter);

        adapters = new LinkedList<>();
    }

    public interface onMenuClickListner{
        void onMenuClick(int id);
    }
}